/**
 * 
 */
package ${basePackage}.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ${basePackage}.model.${className};
import ${basePackage}.model.${className}Example;
import ${basePackage}.service.I${className}Service;
import ${commonPackage}.constant.enums.RestStatus;
import ${commonPackage}.web.DataGridObject;
import ${commonPackage}.web.ResponseObject;


/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("${classNameLower}")
public class ${className}Controller {

	@Autowired
	private I${className}Service ${classNameLower}Service;

	@RequestMapping(value = "to${className}", method = RequestMethod.GET)
	public String to${className}() {
		return "${classNameLower}/${classNameLower}";
	}

	/**
	 * 查询列表页
	 * 
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "search${className}", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<${className}> search${className}(${className} ${classNameLower}, Integer page, Integer rows,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		DataGridObject<${className}> responseObject = new DataGridObject<${className}>();
		${className}Example example = new ${className}Example();		
		if (page == null || page == 0) {
			page = 1;
		}
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);
		
		//如果${classNameLower}有查询值,则需要进行查询

		List<${className}> ${classNameLower}s = ${classNameLower}Service.selectByExample(example);
		responseObject.setTotal(page * rows);
		responseObject.setRows(${classNameLower}s);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/add${className}", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject add${className}(@Validated ${className} ${classNameLower}, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		
		//SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		
		${classNameLower}Service.insert(${classNameLower});
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit${className}", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject edit${className}(@Validated ${className} ${classNameLower}, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
			
		// ----------判断唯一性------------		
		${classNameLower}Service.updateByPrimaryKey(${classNameLower});
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param ${priFormatColumnName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/search${className}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject search${className}(@PathVariable ${priDataType} ${priFormatColumnName}) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (${priFormatColumnName} == null) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		${className} ${classNameLower} = ${classNameLower}Service.selectByPrimaryKey(${priFormatColumnName});
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(${classNameLower});
		return responseObject;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/remove${className}", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject remove${className}(@Validated ${priDataType} ${priFormatColumnName}) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		${className} ${classNameLower} = ${classNameLower}Service.selectByPrimaryKey(${priFormatColumnName});
		${classNameLower}Service.updateByPrimaryKey(${classNameLower});
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

}
